/**
 *
 * @author 陆楚良
 *
 */
class Piece extends egret.Sprite {
    public index: number = 0;
    private _type: number = GV.T_0;
    get type(): number {
        return this._type;
    }
    set type(type: number) {
        if(type !== this._type) {
            this._type = type;
            this.draw();
        }
    }

    private _status: number = GV.S_DEFAULT;
    get status(): number {
        return this._status;
    }
    set status(status: number) {
        if(status !== this._status) {
            switch(this._status) {
                case GV.S_OPEN:
                    this.dispatchEvent(new SaoLeiEvent(SaoLeiEvent.PIECE_CANCEL_OPEN));
                    break;
                case GV.S_DEFAULT:
                    this.dispatchEvent(new SaoLeiEvent(SaoLeiEvent.PIECE_CANCEL_DEFAULT));
                    break;
                case GV.S_BIAOJI:
                    this.dispatchEvent(new SaoLeiEvent(SaoLeiEvent.PIECE_CANCEL_BIAOJI));
                    break;
                case GV.S_YIWEN:
                    this.dispatchEvent(new SaoLeiEvent(SaoLeiEvent.PIECE_CANCEL_YIWEN));
                    break;
            }
            this._status = status;
            switch(status) {
                case GV.S_OPEN:
                    this.dispatchEvent(new SaoLeiEvent(SaoLeiEvent.PIECE_OPEN));
                    break;
                case GV.S_DEFAULT:
                    this.dispatchEvent(new SaoLeiEvent(SaoLeiEvent.PIECE_DEFAULT));
                    break;
                case GV.S_BIAOJI:
                    this.dispatchEvent(new SaoLeiEvent(SaoLeiEvent.PIECE_BIAOJI));
                    break;
                case GV.S_YIWEN:
                    this.dispatchEvent(new SaoLeiEvent(SaoLeiEvent.PIECE_YIWEN));
                    break;
            }
            this.draw();
        }
    }

    private biaoji: egret.Bitmap;
    private yiwen: egret.Bitmap;
    private dilei: egret.Bitmap;
    private tx: egret.TextField;
    private wrong:egret.Shape;
    private _over:Boolean = false;
    public constructor() {
        super();

        this.biaoji = new egret.Bitmap();
        this.biaoji.texture = RES.getRes("sprite.红旗");
        this.biaoji.width = 56 * 20 / 64;
        this.biaoji.height = 20;
        this.biaoji.x = 3;
        this.biaoji.y = 2;

        this.yiwen = new egret.Bitmap();
        this.yiwen.texture = RES.getRes("sprite.问号");
        this.yiwen.width = 48 * 20 / 64;
        this.yiwen.height = 18;
        this.yiwen.x = 5;
        this.yiwen.y = 3;

        this.dilei = new egret.Bitmap();
        this.dilei.texture = RES.getRes("sprite.地雷");
        this.dilei.width = 20;
        this.dilei.height = 20;
        this.dilei.x = 2;
        this.dilei.y = 2;

        this.tx = new egret.TextField();
        this.tx.text = String(this.type);
        this.tx.x = 1;
        this.tx.y = 1;
        this.tx.width = 23;
        this.tx.height = 23;
        this.tx.size = 22;
        this.tx.textColor = 0xAAAAAA;
        this.tx.textAlign = egret.HorizontalAlign.CENTER;
        this.tx.verticalAlign = egret.VerticalAlign.MIDDLE;


        this.wrong = new egret.Shape();
        this.wrong.graphics.lineStyle(2,0xCB0000);
        this.wrong.graphics.moveTo(4,4);
        this.wrong.graphics.lineTo(20,20);
        this.wrong.graphics.moveTo(20,4);
        this.wrong.graphics.lineTo(4,20);
        this.wrong.graphics.endFill();
        
        this.init();
        this.draw();
        this.touchEnabled = true;
        this.addEventListener(egret.TouchEvent.TOUCH_TAP,this.touchHandler,this);
    }

    public init(): void {
        this._over = false;
        this._type = GV.T_0;
        this._status = GV.S_DEFAULT;
        this.draw();
    }
    
    public over():void{
        this._over = true;
        this.draw();
    }
    
    public open(): void {
        if(this.status!==GV.S_BIAOJI){
            this.status = GV.S_OPEN;
        }
    }

    private touchHandler(evt: egret.TouchEvent): void {
        if(this.status !== GV.S_OPEN) {
            if(GV.operate === GV.O_OPEN) {
                if(this.status !== GV.S_BIAOJI) {
                    this.status = GV.S_OPEN;
                }
            } else {
                switch(this.status) {
                    case GV.S_DEFAULT:
                        this.status = GV.S_BIAOJI;
                        break;
                    case GV.S_BIAOJI:
                        this.status = GV.S_YIWEN;
                        break;
                    case GV.S_YIWEN:
                        this.status = GV.S_DEFAULT;
                        break;
                }
            }
        }
    }
    /**
     * 绘制
     */
    private draw(): void {
        this.removeChildren();
        this.graphics.clear();
        this.graphics.beginFill(this.status === GV.S_OPEN ? 0xF3F3F3 : 0x7DB0E0);
        this.graphics.drawRect(0,0,24,24);
        this.graphics.endFill();
        if(this._over && this.type === GV.T_DILEI) {
            this.addChild(this.dilei);
        }
        switch(this.status) {
            case GV.S_BIAOJI:
                this.addChild(this.biaoji);
                break;
            case GV.S_YIWEN:
                this.addChild(this.yiwen);
                break;
            case GV.S_OPEN:
                switch(this.type) {
                    case GV.T_0:
                        break;
                    case GV.T_1:
                    case GV.T_2:
                    case GV.T_3:
                    case GV.T_4:
                    case GV.T_5:
                    case GV.T_6:
                    case GV.T_7:
                    case GV.T_8:
                        this.tx.text = String(this.type);
                        this.addChild(this.tx);
                        break;
                    case GV.T_DILEI:
                        this.addChild(this.dilei);
                        break;
                }
                break;
        }
        if(this._over && this.status===GV.S_BIAOJI && this.type!==GV.T_DILEI){
            this.addChild(this.wrong);
        }
    }
}
